<script lang="ts" setup>
import { reactive, ref, onMounted, watch } from 'vue'
import type { FormItemProps, FormProps, FormInstance, FormRules } from 'element-plus'
import router from '@/router'
//2021010507 黄铄
const labelPosition = ref<FormProps['labelPosition']>('top')
const itemLabelPosition = ref<FormItemProps['labelPosition']>('')
const ruleForm = ref({
  phone: '',
  password: ''
})
const ruleFormRef = ref<FormInstance>()
const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error('请输入手机号'))
  } else {
    if (!/^1[3456789]\d{9}$/.test(value)) {
      callback(new Error('请输入正确的手机号'))
    } else {
      callback()
    }
  }
}
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  }
  callback()
}
const rules = reactive<FormRules<typeof ruleForm>>({
  phone: [{ required: true, validator: checkPhone, trigger: 'blur' }],
  password: [{ required: true, validator: validatePass, trigger: 'blur' }]
})
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('提交成功!')
      router.push({ name: 'IndexView' })
    } else {
      console.log('提交失败!')
    }
  })
}
</script>
<template>
  <div class="loginbac">
    <div class="login">
      <h3>登录</h3>
      <el-form ref="ruleFormRef" :rules="rules" :label-position="labelPosition" :model="ruleForm">
        <el-form-item label="手机号" prop="phone" :label-position="itemLabelPosition">
          <el-input v-model="ruleForm.phone" />
        </el-form-item>
        <el-form-item prop="password" label="密码" :label-position="itemLabelPosition">
          <el-input
            v-model="ruleForm.password"
            placeholder="输入密码"
            show-password
            type="password"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            <router-link to="/">登录
              </router-link> 
              </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.loginbac {
  height: 100vh;
  background-image: url(../assets/xiao.jpg);
  position: relative;
  .login {
    width: 350px;
    // height: 400px;
    background-color: #ffffff93;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
    padding: 25px;
    h3 {
      color: #45474e;
      text-align: center;
      font-size: 20px;
      margin-top: 10px;
    }
    .el-form-item--label-top {
      margin-top: 20px;
      .el-input {
        height: 40px;
      }
      .el-button {
        width: 300px;
        height: 40px;
        margin: 25px 0;
      }
    }
  }
}
</style>
